package gpl.androidcommons.scotlandyard.listener;

import gpl.scotlandyard.beans.basics.Link;
import gpl.scotlandyard.beans.basics.Node;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NodeShowLinksOnClickListener implements OnItemClickListener {

  private final Activity activity;

  public NodeShowLinksOnClickListener(Activity activity) {
    this.activity = activity;
  }

  @Override
  public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
    final Node node = (Node) adapterView.getItemAtPosition(position);
    ListView listView = new ListView(activity);
    listView.setAdapter(new BaseAdapter() {
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv = new TextView(activity);
        Link link = node.getLinks().get(position);
        tv.setText(link.getTicket() + " " + link.getToNode().getNumber());
        return tv;
      }

      @Override
      public long getItemId(int arg0) {
        return 0;
      }

      @Override
      public Object getItem(int arg0) {
        return null;
      }

      @Override
      public int getCount() {
        return node.getLinks().size();
      }
    });

    Dialog dialog = new Dialog(activity);
    dialog.setTitle(node.getNumber() + " [" + node.getLinks().size() + "]");
    dialog.setContentView(listView);
    dialog.show();
  }
}
